<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="老人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入老人姓名"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item label="库存" prop="stockNumber">
        <el-input
          v-model="queryParams.stockNumberBegin"
          @keyup="
            queryParams.stockNumberBegin = (queryParams.stockNumberBegin?.match(
              /[1-9]\d{0,13}(\.\d{0,4})?|0/
            ) ?? [undefined])[0]
          "
          placeholder="请输入起始库存"
          :clearable="true"
          class="!w-32"
        />-
        <el-input
          v-model="queryParams.stockNumberEnd"
          @keyup="
            queryParams.stockNumberEnd = (queryParams.stockNumberEnd?.match(
              /[1-9]\d{0,13}(\.\d{0,4})?|0/
            ) ?? [undefined])[0]
          "
          placeholder="请输入结束库存"
          :clearable="true"
          class="!w-32"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['health:medicineStock:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="HealthMedicineStock"
      height-per="90"
      @page-change="getPage"
    >
      <template #medicineType="{ row }">
        {{ getDictObj(DICT_TYPE.HEALTH_MEDICINE_TYPE, row.medicine.medicineType).label }}
      </template>
      <template #medicineDosageForm="{ row }">
        {{
          getDictObj(DICT_TYPE.HEALTH_MEDICINE_DOSAGE_FORM, row.medicine.medicineDosageForm).label
        }}
      </template>
      <template #menu="{ row }">
        <context-menu-item
          label="数量校正"
          @click="handleMenuAdjust(row)"
          v-hasPermi="['health:medicineStock:adjust']"
        />
        <context-menu-item
          label="退缴"
          @click="handleMenuRefund(row)"
          v-hasPermi="['health:medicineStock:refund']"
        />
      </template>
    </Table>
  </ContentWrap>
  <HealthMedicineStockAdjust ref="medicineStockAdjustRef" @success="getPage" />
</template>
<script setup name="HealthMedicineStock" lang="ts">
import { DICT_TYPE, getDictObj } from '@/utils/dict'
import * as HealthMedicineStockApi from '@/api/health/medicineStock'
import HealthMedicineStockAdjust from './HealthMedicineStockAdjust.vue'
const medicineStockAdjustRef = ref()
const message = useMessage()
const queryFormRef = ref()
const queryParams: any = ref({
  name: undefined,
  stockNumberBegin: undefined,
  stockNumberEnd: undefined
})
const queryData = ref()
const columns = [
  {
    label: '老人姓名',
    prop: 'elder.name'
  },
  {
    label: '药品名称',
    prop: 'medicine.medicineName'
  },
  {
    label: '药品简码',
    prop: 'medicine.medicineBrevityCode'
  },
  {
    label: '药品类型',
    prop: 'medicine.medicineType',
    slot: 'medicineType'
  },
  {
    label: '药品规格',
    prop: 'medicine.medicineSpecification'
  },
  {
    label: '药品剂型',
    prop: 'medicine.medicineDosageForm',
    slot: 'medicineDosageForm'
  },
  {
    label: '库存',
    prop: 'stockNumber'
  }
]
/** 初始化 */
onMounted(() => {
  handleClickSearch()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await HealthMedicineStockApi.pageQuery(queryParams.value)
}

/** 处理右击事件 数量校正 */
const handleMenuAdjust = (row) => {
  medicineStockAdjustRef.value.open(row)
}

/** 处理右击事件 退缴 */
const handleMenuRefund = async (row) => {
  await message.confirm('确定要退缴吗？')
  await HealthMedicineStockApi.refund(row.id)
  getPage()
}
</script>
